import 'package:event_bus/event_bus.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBCommonToast/YBCommonToastPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginMainPage/YBLoginProvider/YBEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginHeaderInfoPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginOtherPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginSubPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginYinSiPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBLoginInfoModule/YBLoginInfoApi.dart';

import 'YBLoginProvider/YBLoginSelectEventBus.dart';


class YBLoginMainPage extends StatefulWidget {
  @override
  _YBLoginMainPageState createState() => _YBLoginMainPageState();
}

class _YBLoginMainPageState extends State<YBLoginMainPage> {

  String _phoneNumber;
  bool loginSelectBool = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    eventBus.on<YBPhoneEventBus>().listen((event) {
      print('----${event.phoneNumber}');
       setState(() {
         _phoneNumber = event.phoneNumber;
       });

    });

    selectEventBus.on<YBLoginSelectEventBus>().listen((event) {
      loginSelectBool = event.loginSelect;
    });

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: YBColor.white,
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          leading: InkWell(
            child: Image.asset(YBImageStyle.getImagePath('login_close')),
            onTap: (){
              Navigator.pop(context);
            },
          ),
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.only(left: 20,right: 20,top: 10,bottom: 10),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Container(
                  child: YBLoginHeaderInfoPage(),
                ),
                Container(
                  height: 30,
                ),
                Container(
                  child: Text("登录/注册",style: YBTextStyle.login1C1D1F28SemiBold,),
                ),
                Container(
                  height: 30,
                ),
                Container(
                  child: YBLoginSubPage(),
                ),
                Container(
                  height: 30,
                ),
                Container(
                  height: 44,
                  child: RawMaterialButton(
                      child: Text("登录",style:YBTextStyle.xieGangQingNianTuiGuangYuanBtnText,),
                      fillColor: YBColor.commonThemeColor,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(22.0)),
                      ),
                      onPressed:(){
                        if (loginSelectBool == false) {
                          YBToast.showToastInfo('您没有同意协议');
                          return;
                        }
                        _initLogin(_phoneNumber, context);
                      }
                  ),
                ),
                Container(
                  height: 20,
                ),
                Container(
                  child: YBLoginYinSiPage(),
                ),
                Container(
                  child:YBLoginOtherPage(),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _initLogin(String phoneNumber,BuildContext context){
    EasyLoading.show(status: 'loading...');
    YBLoginInfoApi.loginInMsgCode({"phone":phoneNumber,'sysCode':"xg_youbang"},success:(data){
      print('----$data----');
      var msgTokenMap = data;
      //msgToken
      EasyLoading.dismiss();
      print('msgToken---$msgTokenMap');
      if (msgTokenMap['msgToken'].toString().isNotEmpty) {
        YBToast.showToastInfo("短信已发送，请注意查收");
        YBApplication.router.navigateTo(context, '/loginIn/loginYanZhengMaPage?msgToken=${msgTokenMap['msgToken']}&&phoneN=$phoneNumber&&type=1');
      }
    },fail: (error){
      print("---$error---");
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
   // eventBus.destroy();
    super.dispose();
  }

}





